package com.xl.web.controller.message;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xl.base.annotation.Auth;
import com.xl.base.annotation.Duplicate;
import com.xl.base.web.BaseController;
import com.xl.core.util.SessionLocalUtils;
import com.xl.web.entity.message.MessageBase;
import com.xl.web.entity.sys.SysMember;
import com.xl.web.service.message.MessageBaseService;
import com.xl.web.service.sys.SysMemberService;

import jodd.util.StringUtil;

/**
 * 前台站内信 tb_mj_msg
 * 
 * @author 梁华
 * 
 */
@Controller
@RequestMapping("business/message")
@Scope("prototype")
public class  MessageController extends BaseController {

	@Autowired
	private MessageBaseService<MessageBase> messageBaseService;
	// 注入系统人员管理service
	@Autowired(required = false)
	private SysMemberService<SysMember> sysMemberService;
	

	
	/**
	 * 获取该用户所有站内信
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true,permissions = "authority.message.list")
	@RequestMapping("list")
	public ModelAndView listInfo(MessageBase messageBase,HttpServletRequest request) throws Exception {
		Map<String, Object> context = getRootMap(request);
		context.put("messageBase",messageBase);
		return forword("message/message_list", context);
	}
	
	
	/**
	 * 站内信列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "authority.message.list")
	@ResponseBody
	@RequestMapping("/listData")
	public void listData() throws Exception {
		MessageBase messageBase = (MessageBase)getJsonParams(MessageBase.class);
		SysMember member = SessionLocalUtils.getOperator().getMember();
		messageBase.setMemberId(member.getId());
		messageBase.getPager().setOrderField("t.create_date desc");
		List<MessageBase>  dataList= messageBaseService.queryList(messageBase);
		sendListData(response, messageBase.getPager().getRowCount(), dataList);
	}
	
	
	/**
	 * 添加站内信
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "authority.message.add")
	@Duplicate(add = true)
	@RequestMapping("/add")
	public ModelAndView add() throws Exception {
		Map<String, Object> context = getRootMap(request);
		context.put("memberId", SessionLocalUtils.getOperator().getMember().getId()) ;
		return forword("message/message_add", context);
	}
	
	/**
	 * 添加站内信
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "authority.message.add")
	@Duplicate(add = true)
	@RequestMapping("/view")
	public ModelAndView view(String id) throws Exception {
		Map<String, Object> context = getRootMap(request);
		//context.put("memberId",) ;
		
		String memberId= SessionLocalUtils.getOperator().getMember().getId();
		MessageBase messageBase =messageBaseService.queryById(id);
		if(messageBase.getReceiveId().equals(memberId)){
			if(!messageBase.getStatus().equals("2")){//未读改成已读
				messageBase.setStatus("2");//已读
				messageBaseService.updateBySelective(messageBase);
			}
			SysMember member= sysMemberService.queryById(messageBase.getSendId());
			context.put("receive", "true");
			context.put("receiveName", member.getMemName());
		}else if(messageBase.getSendId().equals(memberId)){
			SysMember member= sysMemberService.queryById(messageBase.getReceiveId());
			context.put("sendName", member.getMemName());
			context.put("send", "true");
		}
		
		{
			MessageBase messageBase1 =new MessageBase();
			messageBase1.setReplyMessageId(messageBase.getId());
			messageBase1.getPager().setOrderField("t.create_date");
			List<MessageBase> dataList=messageBaseService.queryByList(messageBase1);
			if(dataList!=null && dataList.size()!=0){
				SysMember receiveMember= sysMemberService.queryById(messageBase.getReceiveId());
				SysMember sendMember= sysMemberService.queryById(messageBase.getSendId());
				if(memberId.equals(messageBase.getReceiveId())){
					receiveMember.setMemName("mi");
				}else{
					sendMember.setMemName("mi");
				}
				for(MessageBase i:dataList){
					if(i.getReceiveId().equals(receiveMember.getId())){
						i.setReceiveName(receiveMember.getMemName());
						i.setSendName(sendMember.getMemName());
					}else{
						i.setReceiveName(sendMember.getMemName());
						i.setSendName(receiveMember.getMemName());
					}
					if(i.getStatus().equals("1")&&i.getReceiveId().equals(memberId)){
						MessageBase entity =new MessageBase();
						entity.setStatus("2");
						entity.setId(i.getId());
						messageBaseService.updateBySelective(entity);
					}
				}
			}
			context.put("dataList", dataList);
		}
		
		context.put("messageBase", messageBase);
		return forword("message/message_view", context);
	}
	
	/**
	 * 修改内信
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "authority.message.handle")
	@Duplicate(add = true)
	@RequestMapping("/update")
	public ModelAndView update(String id) throws Exception {
		Map<String, Object> context = getRootMap(request);
		context.put("memberId", SessionLocalUtils.getOperator().getMember().getId()) ;
		MessageBase messageBase =messageBaseService.queryById(id);
		SysMember member= sysMemberService.queryById(messageBase.getReceiveId());
		messageBase.setReceiveName(member.getMemName());
		context.put("messageBase", messageBase);
		return forword("message/message_update", context);
	}
	/**
	 * 保存站内信
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "authority.message.add")
	@Duplicate(add = true)
	@RequestMapping("/messageSave")
	public void messageSave(MessageBase messageBase) throws Exception {
		if(StringUtil.isBlank(messageBase.getId())){
			messageBase.setSendId(SessionLocalUtils.getOperator().getMember().getId());
		}
		if("1".equals(messageBase.getStatus())){
			messageBase.setSendDate(new Date());
		}
		messageBaseService.saveOrUpdate(messageBase);
		sendSuccessMessage(response, "操作成功");
	}	
	
	
	/**
	 * 回复信息保存
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "authority.message.add")
	@Duplicate(add = true)
	@RequestMapping("/replyMessageSave")
	public void replyMessageSave(MessageBase messageBase) throws Exception {
		MessageBase messageBase1 =messageBaseService.queryById(messageBase.getId());
		String memberId= SessionLocalUtils.getOperator().getMember().getId();
		messageBase.setSendId(memberId);
		if(messageBase1.getReceiveId().equals(memberId)){
			messageBase.setReceiveId(messageBase1.getSendId());
		}else{
			messageBase.setReceiveId(messageBase1.getReceiveId());
		}
		messageBase.setSendDate(new Date());
		messageBase.setReplyMessageId(messageBase.getId());
		messageBase.setStatus("1");
		messageBase.setId(null);
		messageBaseService.saveOrUpdate(messageBase);
		sendSuccessMessage(response, "回复成功");
	}	
	
	/**
	 * 系统用户
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("sysUsers")
	public ModelAndView sysMemList(HttpServletRequest request) throws Exception{
		Map<String, Object> context = getRootMap(request);
		return forword("message/get_member", context);
	}
	
	/**
	 * 系统用户列表数据
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/memberListData")
	public void memberListData() throws Exception {
		SysMember member = (SysMember)getJsonParams(SysMember.class);
		List<SysMember> dataList = sysMemberService.queryByList(member);
		sendListData(response, member.getPager().getRowCount(), dataList);
	}
}